$(document).ready(function() {
	log = log4javascript.getDefaultLogger();
	// 鏋勯�缁勪欢锛屾ā鍨嬶紝寤虹珛缁勪欢妯″瀷涔嬮棿鐨勪竴瀵逛竴鍏崇郴锛岀粰缁勪欢璁剧疆妯℃澘鍏冪礌
	$("[component]").each(function(i) {
		var comp = newObj(this);
	});
});

/**
 * 鏍规嵁json鏍煎紡涓插垱寤哄璞★紝灞炴�c鏄瀯閫犲嚱鏁板悕, 灞炴�id鏄璞″悕
 * 
 * @param str
 *            json鏍煎紡涓�
 * @return 鎵ц鏋勯�鍑芥暟鍚庣殑瀵硅薄
 */
function newObj(comp) {
	var str = $(comp).attr("component")
	var obj = eval("(" + str + ")");
	var item = "";
	$("[item" + obj.id + "]", comp).each(function(i) {
		var child = newObj(this);
		if (item == "") {
			item += "[";
		} else {
			item += ",";
		}
		item += child;
	});
	if (item != "") {
		obj.items = item;
	}
	var attr = proToString(obj);
	eval("var cons=new " + obj.c + "(" + attr + ")");
	alert("comp:" + cons);
	return cons;
}

function proToString(obj) {
	var props = "{";
	for (var p in obj) {
		var attr = "";
		if (typeof(obj[p]) == "function") {
			// obj[p]();
			continue;
		} else if (typeof(obj[p]) == "object") {
			attr = p + ":'" + proToString(obj[p]) + "'";
		} else if (typeof(obj[p] == "boolean")) {
			if (obj[p]) {
				attr = p + ":true";
			} else {
				attr = p + ":false";
			}
		} else {
			attr = p + ":'" + obj[p] + "'";
		}
		if (props != "{" && attr != "") {
			props += ",";
		}
		props += attr;
	}
	alert
	return props += "}";
}

/**
 * 鐣岄潰缁勪欢锛屽彲浠ユ湁1涓�鎴栬�0涓暟鎹ā鍨�
 */
function Component() {
	/**
	 * 妯℃澘鍏冪礌
	 */
	this.element;

	/**
	 * 鏁版嵁妯″瀷
	 */
	this.bind;

	/**
	 * 缁勪欢鐨勭埗缁勪欢
	 */
	this.parent;

	/**
	 * 缁勫缓鐨勫悕瀛�this.c;
	 */

	/**
	 * 缁勪欢缁樺埗 this.paint = function() {}
	 */

	/**
	 * 缁勫缓鏇存柊 this.update = function(){}
	 */

	/**
	 * 缁勪欢鍒濆鍖�
	 */
	this.init = function() {
	}

	/**
	 * 璁剧疆缁勫缓鐨勫厓绱�
	 */
	this.setElement = function(elem) {
		this.element = elem;
		this.setId().setBind();
		return this;
	}

	/**
	 * 璁剧疆鏁版嵁妯″瀷锛屽缓绔嬬粍浠朵笌鏁版嵁妯″瀷涔嬮棿鐨勪竴瀵逛竴鍏崇郴
	 */
	this.setBind = function() {
		// 濡傛灉鏈塨inding鍐呭锛屼篃灏辨槸鏈夋暟鎹ā鍨嬶紝鍒涘缓鏁版嵁妯″瀷
		var str = $(this.element).attr("binding");
		if (str) {
			this.bind = newObj(str);
			this.bind.setComponent(this);
		}
		return this;
	}

	/**
	 * 璁剧疆鐖剁粍浠�
	 */
	this.setParent = function(comp) {
		this.parent = comp;
		return this;
	}

	/**
	 * 璁剧疆缁勪欢鐨勭被鍨�
	 */
	this.setObjectType = function(string) {
		this.c = string;
		return this;
	}

	/**
	 * 璁剧疆缁勪欢鍚�
	 */
	this.setId = function() {
		var comp = eval("(" + $(this.element).attr("component") + ")");
		if (comp.id) {
			this.id = comp.id;
		}
		return this;
	}
}

/**
 * 鏁版嵁妯″瀷锛屾暟鎹ā鍨嬩笌缁勪欢涔嬮棿鏄竴瀵逛竴鍏崇郴
 */
function Bind() {
	/**
	 * 瀵瑰簲鐨勭粍浠�
	 */
	this.component;

	this.setComponent = function(comp) {
		this.component = comp;
		return this;
	}

	/**
	 * 鏇存柊瀵瑰簲缁勪欢
	 */
	this.update = function() {
		this.component.paint();
		return this;
	}
}